//
//  BCStuffThirdCollectionViewCell.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/6/25.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCStuffThirdCollectionViewCell.h"

@interface BCStuffThirdCollectionViewCell ()<MMNumberKeyboardDelegate> {
    UIImageView *stuffImgView;
    UILabel *nameLab;
    UILabel *priceLab;
    UILabel *priceLab1;
    UILabel *priceLab2;
    UITextField *amountLab;
    UIView *backView;
    
    UIButton *style1;
    UIButton *style2;
    UIButton *style3;
    
    UILabel *detailLab;

}
@end

@implementation BCStuffThirdCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView {
    static NSString *ID = @"StuffThirdCell";
    [collectionView registerClass:[BCStuffThirdCollectionViewCell class] forCellWithReuseIdentifier:ID];
    BCStuffThirdCollectionViewCell *cell = [[BCStuffThirdCollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 118.f)];
    return cell;
}

- (void)createView
{
    
    stuffImgView = [[UIImageView alloc] init];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gopush:)];
    [stuffImgView addGestureRecognizer:tap];
    stuffImgView.userInteractionEnabled = YES;
    nameLab = [[UILabel alloc] init];
    nameLab.font = [UIFont systemFontOfSize:16];
    priceLab = [[UILabel alloc] init];
    priceLab.textColor = [UIColor colorWithRed:0.908  green:0.112  blue:0.114 alpha:1];
    priceLab1 = [[UILabel alloc] init];
    priceLab2 = [[UILabel alloc] init];
    
    style1 = [UIButton buttonWithType:UIButtonTypeSystem];
    style2 = [UIButton buttonWithType:UIButtonTypeSystem];
    style3 = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [style1 setBackgroundColor:[UIColor whiteColor]];
    [style2 setBackgroundColor:[UIColor whiteColor]];
    [style3 setBackgroundColor:[UIColor whiteColor]];
    
    style1.layer.borderWidth = 1;
    style1.layer.borderColor = [UIColor colorWithRed:0.061  green:0.747  blue:0.338 alpha:1].CGColor;
    style1.layer.cornerRadius = 5;
    style2.layer.borderWidth = 1;
    style2.layer.borderColor = [UIColor colorWithRed:0.061  green:0.747  blue:0.338 alpha:1].CGColor;
    style2.layer.cornerRadius = 5;
    style3.layer.borderWidth = 1;
    style3.layer.borderColor = [UIColor colorWithRed:0.061  green:0.747  blue:0.338 alpha:1].CGColor;
    style3.layer.cornerRadius = 5;
    
    style1.titleLabel.font = [UIFont systemFontOfSize:12];
    style1.tintColor = [UIColor colorWithRed:0.361  green:0.821  blue:0.536 alpha:1];
    style2.titleLabel.font = [UIFont systemFontOfSize:12];
    style2.tintColor = [UIColor colorWithRed:0.361  green:0.821  blue:0.536 alpha:1];
    style3.titleLabel.font = [UIFont systemFontOfSize:12];
    style3.tintColor = [UIColor colorWithRed:0.361  green:0.821  blue:0.536 alpha:1];
    
    
    [style1 setTitle:@"高级" forState:UIControlStateNormal];
    [style2 setTitle:@"一级" forState:UIControlStateNormal];
    [style3 setTitle:@"二级" forState:UIControlStateNormal];
    detailLab = [[UILabel alloc] init];
    detailLab.textColor = [UIColor lightGrayColor];
    detailLab.numberOfLines = 0;
    detailLab.font = [UIFont systemFontOfSize:14];
    
    priceLab.textColor = [UIColor colorWithRed:0.998  green:0.371  blue:0 alpha:1];
    priceLab1.textColor = [UIColor colorWithRed:0.998  green:0.371  blue:0 alpha:1];
    priceLab2.textColor = [UIColor colorWithRed:0.533  green:0.533  blue:0.533 alpha:1];
    priceLab.text = @"¥";
    priceLab.textAlignment = NSTextAlignmentRight;
        priceLab.font = [UIFont systemFontOfSize:12];
    priceLab1.font = [UIFont systemFontOfSize:18];
    priceLab2.font = [UIFont systemFontOfSize:12];
    amountLab = [[UITextField alloc] init];
    amountLab.font = [UIFont systemFontOfSize:16];
    amountLab.textAlignment = NSTextAlignmentCenter;
    
    // Create and configure the keyboard.
    MMNumberKeyboard *keyboard = [[MMNumberKeyboard alloc] initWithFrame:CGRectZero];
    keyboard.returnKeyTitle = @"确认";
    NSDictionary *dic = [keyboard valueForKey:@"buttonDictionary"];
    [dic[@(12)] setValue:BangcaiGreen forKey:@"fillColor"];
    keyboard.allowsDecimalPoint = NO;
    keyboard.delegate = self;
    amountLab.inputView = keyboard;
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40.f)];
    accessoryView.backgroundColor = [UIColor whiteColor];
    UILabel *keyboardTipLable = [[UILabel alloc] initWithFrame:accessoryView.bounds];
    keyboardTipLable.font = [UIFont systemFontOfSize:13];
    keyboardTipLable.textColor = BangcaiGreen;
    keyboardTipLable.text = @"请输入欲购买的数量";
    keyboardTipLable.textAlignment = NSTextAlignmentCenter;
    [accessoryView addSubview:keyboardTipLable];
    amountLab.inputAccessoryView = accessoryView;
    
    [amountLab addTarget:self action:@selector(inputNum) forControlEvents:UIControlEventEditingDidEnd];
    amountLab.keyboardType = UIKeyboardTypeNumberPad;
    [amountLab addTarget:self action:@selector(closeUserEnabel) forControlEvents:UIControlEventEditingDidBegin];
    self.plus = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.plus setImage:[UIImage imageNamed:@"加"] forState:UIControlStateNormal];
    self.minus = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.minus setImage:[UIImage imageNamed:@"减"] forState:UIControlStateNormal];
    
    backView = [[UIView alloc] init];
    
    [backView addSubview:stuffImgView];
    [backView addSubview:nameLab];
    [backView addSubview:priceLab];
    [backView addSubview:priceLab1];
    [backView addSubview:priceLab2];
    [backView addSubview:amountLab];
    [backView addSubview:style1];
    [backView addSubview:style2];
    [backView addSubview:style3];
    [backView addSubview:self.plus];
    [backView addSubview:self.minus];
    [backView addSubview:style1];
    [backView addSubview:style2];
    [backView addSubview:style3];
    [backView addSubview:detailLab];
    
    
    [self.contentView addSubview:backView];
    self.amount = 0;
    [self.plus addTarget:self action:@selector(plusAction) forControlEvents:UIControlEventTouchUpInside];
    [self.minus addTarget:self action:@selector(minusAction) forControlEvents:UIControlEventTouchUpInside];
    backView.backgroundColor = [UIColor whiteColor];
    stuffImgView.image = [UIImage imageNamed:@"占位图"];

    
}

- (void)inputNum{
    if (![_name isEqualToString:@"演示数据"]) {
        
    
    NSInteger amount = [amountLab.text integerValue];
    self.amount = amount;
    [[BCShopCarManager sharedInstance] setStuffName:self.name amount:amount price:145.0 type:0];
    [BCOrderViewManager writeStuffUseCountToOrder:_stuff.uuid Count:amount];
    self.plus.userInteractionEnabled = YES;
    self.minus.userInteractionEnabled = YES;
    
    [self.delegate reloadData];
    }
}

- (void)closeUserEnabel{
    self.plus.userInteractionEnabled = NO;
    self.minus.userInteractionEnabled = NO;
}

- (void)plusAction
{
    if (![_name isEqualToString:@"演示数据"]) {
        
    
    self.amount += 1;
    [[BCShopCarManager sharedInstance] addStuffName:self.name amount:1 price:_price type:0];
     [BCOrderViewManager updateAddStuffToOrder:_stuff];
    [self.delegate reloadData];
    _reddot();
    }
}

- (void)minusAction{
    if (![_name isEqualToString:@"演示数据"]) {
        
    
    self.amount -= 1;
    [[BCShopCarManager sharedInstance] removeStuffName:self.name amount:1 price:_price type:0];
    [BCOrderViewManager updateDecStuffToOrder:_stuff];

    [self.delegate reloadData];
    }
}

- (void)setStuffImg:(UIImage *)stuffImg
{
    _stuffImg = stuffImg;
    stuffImgView.image = stuffImg;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (![_name isEqualToString:@"演示数据"]) {
        priceLab1.text = [NSString stringWithFormat:@"%@", [_stuff.price rounding:2]];
        priceLab2.text = [NSString stringWithFormat:@"/%@", _stuff.unit];
        
    }else{
        priceLab1.text = @"0";
        priceLab2.text = @"/0斤";

    }
    self.detailInfo = @"演示数据演示数据演示数据演示数据演示数据演示数据";
    detailLab.text = self.detailInfo;
    nameLab.text = _name;
    if (self.amount > 0) {
        amountLab.text = [NSString stringWithFormat:@"%ld", self.amount];
    }else{
        amountLab.text = @"";
    }
    self.minus.hidden = !_minusHidden;
    
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.and.bottom.equalTo(self.contentView);
        
    }];
    
    [stuffImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.and.width.mas_equalTo(70 * wx);
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(15 * wx);
    }];
    
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(stuffImgView.mas_top);
        make.left.mas_equalTo(stuffImgView.mas_right).with.offset(12);
        make.height.mas_equalTo(16);
        make.width.mas_equalTo(200 * wx);
    }];
    
    [amountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-40 * wx);
        make.bottom.mas_equalTo(-12);
        make.width.and.height.mas_equalTo(30 * wx);
    }];
    
    [priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(amountLab);
        make.left.equalTo(nameLab).with.offset(-5);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(17);
    }];
    [priceLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(priceLab);
        make.left.mas_equalTo(priceLab.mas_right);
        make.height.mas_equalTo(20);
    }];
    
    [priceLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceLab);
        make.left.mas_equalTo(priceLab1.mas_right);
        make.height.mas_equalTo(15);
    }];
    
    [style1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nameLab.mas_bottom).with.offset(12);
        make.left.equalTo(nameLab);
        make.width.mas_equalTo(60 * wx);
        make.height.mas_equalTo(27);
    }];
    [style2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nameLab.mas_bottom).with.offset(12);
        make.left.mas_equalTo(style1.mas_right).with.offset(12);
        make.width.mas_equalTo(style1.mas_width);
        make.height.mas_equalTo(27);
    }];
    [style3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nameLab.mas_bottom).with.offset(12);
        make.left.mas_equalTo(style2.mas_right).with.offset(12);
        make.width.mas_equalTo(style1.mas_width);
        make.height.mas_equalTo(27);
    }];

    [self.plus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(amountLab.mas_right);
        make.bottom.mas_equalTo(amountLab.mas_bottom);
        make.height.and.width.mas_equalTo(27 * wx);
        
    }];
    
    [self.minus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(amountLab.mas_left);
        make.size.and.top.equalTo(self.plus);
        
    }];
    
    [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nameLab.mas_bottom).with.offset(8);
        make.left.equalTo(nameLab);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(35);
    }];
    
    switch (self.cellStyle) {
        case 0:{
            style1.hidden = NO;
            style2.hidden = NO;
            style3.hidden = NO;
            detailLab.hidden = YES;
        }
            break;
        case 1:{
            style1.hidden = NO;
            style2.hidden = YES;
            style3.hidden = YES;
            detailLab.hidden = YES;
        }
            break;
        case 2:{
            style1.hidden = YES;
            style2.hidden = YES;
            style3.hidden = YES;
            detailLab.hidden = NO;
        }
            break;
        case 3:{
            style1.hidden = YES;
            style2.hidden = YES;
            style3.hidden = YES;
            detailLab.hidden = YES;
        }
            break;
        default:
            break;
    }
    
}
- (void)gopush:(UITapGestureRecognizer*)tap{
    [self.delegate pushWithUUID:self.stuff.uuid];
}

-(BOOL)numberKeyboardShouldReturn:(MMNumberKeyboard *)numberKeyboard {
    return YES;
}

@end
